#include <event2/event.h>
#include <event2/listener.h>
#include <iostream>
#include <signal.h>
#include <assert.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
using namespace std;

static timeval t1{1, 0};    // 1s 的定时器

static void timer1(int sockfd, short event_type, void *args) {
    cout << "[timer - 1]" << flush;
    event *ev = (event*)args;
    if (!evtimer_pending(ev, &t1)) {
        evtimer_del(ev);
        evtimer_add(ev, &t1);
    }
}

int main() {
    event_base *base = event_base_new();

    event *ev1 = evtimer_new(base, timer1, event_self_cbarg());
        if (!ev1) {
        cerr << "evtimer_new fail" << endl;
        return 1;
    }
    // 添加事件到 pending 状态
    if (evtimer_add(ev1, &t1)) {
        cerr << "evtimer_add fail" << endl;
        return 1;
    }

    // t3 = event_base_init_common_timeout(base, &t1);
    // 使用 t3 会将计时器从时间堆切换到双向链表

    // 进入事件主循环
    event_base_dispatch(base);
    event_free(ev1);
    event_base_free(base);
    // 添加 ctrlC 事件后先挂起再杀进程 pkill -9 a.out

}